/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Data;

namespace DNNContentBuilder 
{
	[Serializable]
	public class FieldDefinitionInfo 
	{
        
		// Entity member variables for database table epFieldDefinition
        
		private Int32 mFieldDefinitionId;
		private string mTitle;
		private string mCssClass;
		private bool mSearch;
		private string mTooltip;
		private bool mRequired;
		private string mSourceField;
		private Int32 mFormDefinitionId;
		private Int32 mControlTypeId;
		private Int32 mTabIndex;
		private string mXML;
		private bool mVisible;
		private bool mIsTitle;
		private Int32 mDataConnectionId;
        
		// Calculated Expression member variables for database table epFieldDefinition
        
        
		// Entity properties for database table epFieldDefinition
        
		public Int32 FieldDefinitionId 
		{
			get { return mFieldDefinitionId; }
			set { mFieldDefinitionId = value; }
		}
        
		public Int32 DataConnectionId 
		{
			get { return mDataConnectionId; }
			set { mDataConnectionId = value; }
		}
        
		public string Title 
		{
			get { return mTitle; }
			set { mTitle = value; }
		}
        
        
		public string CssClass 
		{
			get { return mCssClass; }
			set { mCssClass = value; }
		}
        
        
		public bool Search 
		{
			get { return mSearch; }
			set { mSearch = value; }
		}
        
		public bool Visible 
		{
			get { return mVisible; }
			set { mVisible = value; }
		}

		public bool IsTitle 
		{
			get { return mIsTitle; }
			set { mIsTitle = value; }
		}

		/// <summary>
		/// Gets whether this is a linker control field definition
		/// </summary>
		public bool IsLinker
		{
			get
			{
				return this.XML.IndexOf("<Linker") != -1;
			}
		}

		public string Tooltip 
		{
			get { return mTooltip; }
			set { mTooltip = value; }
		}
        
		public bool Required 
		{
			get { return mRequired; }
			set { mRequired = value; }
		}
        
		public string SourceField 
		{
			get 
			{ 
				return mSourceField; 
			}
			set 
			{ 
				mSourceField = value; 
			}
		}

		/// <summary>
		/// Gets the name of the database table that the source field belongs to.
		/// </summary>
		public string TableName
		{
			get
			{
				if (this.SourceField.IndexOf('.') != -1)
				{
					return SourceField.Split('.')[0];
				}

				return String.Empty;
			}
		}

		/// <summary>
		/// Gets the name of field
		/// </summary>
		public string FieldName
		{
			get
			{
				if (this.SourceField.IndexOf('.') != -1)
				{
					return SourceField.Split('.')[1];
				}

				return this.SourceField;
			}
		}
       
		public Int32 FormDefinitionId 
		{
			get { return mFormDefinitionId; }
			set { mFormDefinitionId = value; }
		}
        
        
		public Int32 ControlTypeId 
		{
			get { return mControlTypeId; }
			set { mControlTypeId = value; }
		}

		public Int32 TabIndex 
		{
			get { return mTabIndex; }
			set { mTabIndex = value; }
		}

		public string XML
		{
			get { return mXML; }
			set { mXML = value; }
		}

		/// <summary>
		/// Gets whether this field is a primary key field
		/// </summary>
		public bool IsPrimaryKey
		{
			get { return XML.IndexOf("PrimaryKey") > 0; }
		}
	}
    
	public class FieldDefinitionController : BaseController
	{
		public FieldDefinitionController() : base() {}
		public FieldDefinitionController(ConnectionProvider cp) : base(cp) {}
        
		public Int32 Add(FieldDefinitionInfo FieldDefinitionInfo) 
		{
			return (Int32) base.GetDataProvider().FieldDefinitionAdd(FieldDefinitionInfo.Title, FieldDefinitionInfo.CssClass, FieldDefinitionInfo.Search, FieldDefinitionInfo.Tooltip, FieldDefinitionInfo.Required, FieldDefinitionInfo.SourceField, FieldDefinitionInfo.FormDefinitionId, FieldDefinitionInfo.ControlTypeId, FieldDefinitionInfo.TabIndex, FieldDefinitionInfo.XML, FieldDefinitionInfo.Visible, FieldDefinitionInfo.IsTitle, FieldDefinitionInfo.DataConnectionId);
		}
        
		public void Update(FieldDefinitionInfo FieldDefinitionInfo) 
		{
			base.GetDataProvider().FieldDefinitionUpdate(FieldDefinitionInfo.FieldDefinitionId, FieldDefinitionInfo.Title, FieldDefinitionInfo.CssClass, FieldDefinitionInfo.Search, FieldDefinitionInfo.Tooltip, FieldDefinitionInfo.Required, FieldDefinitionInfo.SourceField, FieldDefinitionInfo.FormDefinitionId, FieldDefinitionInfo.ControlTypeId, FieldDefinitionInfo.XML, FieldDefinitionInfo.Visible, FieldDefinitionInfo.IsTitle, FieldDefinitionInfo.DataConnectionId);
		}

		public void DeleteAllFieldDefinitions(Int32 formDefinitionId) 
		{
			base.GetDataProvider().FieldDefinitionDeleteAllFieldDefinitions(formDefinitionId);
		}
        
		public void Delete(Int32 fieldDefinitionId) 
		{
			base.GetDataProvider().FieldDefinitionDelete(fieldDefinitionId);
		}
        
		public FieldDefinitionInfo GetItem(Int32 fieldDefinitionId) 
		{
			using (IDataReader idr = base.GetDataProvider().FieldDefinitionGet(fieldDefinitionId))
			{
				FieldDefinitionInfo infoObject = (FieldDefinitionInfo)DotNetNuke.Common.Utilities.CBO.FillObject(idr, typeof(FieldDefinitionInfo));
				return infoObject;
			} 
		}
        
		// Select query methods for database table epFieldDefinition
		public ArrayList GetFieldDefinitionControlTypes(Int32 formDefinitionId) 
		{
			using (IDataReader idr = base.GetDataProvider().FieldDefinitionGetFieldDefinitionControlTypes(formDefinitionId))
			{
				ArrayList infoList = DotNetNuke.Common.Utilities.CBO.FillCollection(idr, typeof(FieldDefinitionInfo));
				return infoList;
			} 
		}
        
		// Relation methods for database table epFieldDefinition
		public ArrayList GetByControlType(Int32 controlTypeId) 
		{
			using (IDataReader idr = base.GetDataProvider().FieldDefinitionByControlType(controlTypeId))
			{
				ArrayList infoList = DotNetNuke.Common.Utilities.CBO.FillCollection(idr, typeof(FieldDefinitionInfo));
				return infoList;
			} 
		}
        
		public ArrayList GetByFormDefinition(Int32 formDefinitionId) 
		{
			using (IDataReader idr = base.GetDataProvider().FieldDefinitionByFormDefinition(formDefinitionId))
			{
				ArrayList infoList = DotNetNuke.Common.Utilities.CBO.FillCollection(idr, typeof(FieldDefinitionInfo));
				return infoList;
			} 
		}
	}
}
